/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-13 16:06:52
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-24 10:21:20
 * @FilePath: \cxl-driving-training\src\router\limit.ts
 */
import { userStore } from '@/store/user.store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from '.'
import { routerStore } from '@/store/router.store'
import { storage } from '@/store/sessionStorage'

class Limit {
    _userStore: any
    _routerStore: any
    constructor() {
        //初始化用户数据仓库
        this._userStore = userStore()
        //初始化路由数据仓库
        this._routerStore = routerStore()
    }

    init() {
        // 路由跳转前判断用户是否登录
        router.beforeEach((to, from, next) => {
            //页面顶部进度条加载开始
            NProgress.start()

            //从store中取出token，判断是否为空 ，为空则跳转登录页面
            if (this._userStore.token === '') {
                //根据当前要跳转的页面是否是登录页面（login）,来判断是否传参
                if (to.name === 'login') {
                    next()
                } else {
                    next({ name: 'login' })
                }
                storage.clear()
            } else {
                if (!this._routerStore.routeArray.length) {
                    this._routerStore.getRoute().then((arr: any[]) => {
                        arr.forEach((item: any) => {
                            if (item.path === '' && item.children.length) {
                                //item.path = item.children[0].path
                            }
                            router.addRoute(item)
                        })
                        if (!arr.length) {
                            storage.clear()
                            next({ name: 'login' })
                        } else {
                            next({ ...to, replace: true })
                        }

                        NProgress.done()
                        // 设置顶部的动态菜单
                        this.setRoute(to)
                    })
                } else {
                    if (to.name) {
                        next()
                    } else {
                        next({ name: 'index' })
                    }

                    // 设置顶部的动态菜单
                    // console.log('设置顶部的动态菜单', to)
                    this.setRoute(to)
                }
            }
        })
        router.afterEach(() => {
            //页面顶部进度条加载结束
            NProgress.done()
        })
    }
    setRoute = (to: any) => {
        if (to.name) {
            let title: any = ''
            if (to.name === 'class-operation') {
                title = to.query.type === 'create' ? '新增班别' : to.query.type === 'edit' ? '班别编辑' : '班别详情'
            } else if (to.name === 'driving-school-operation') {
                title = to.query.type === 'create' ? '新增驾校' : '驾校编辑'
            } else if (to.name === 'coach-operation') {
                title = to.query.type === 'create' ? '新增教练' : '教练编辑'
                console.log('to', to.query)
            } else if (to.name === 'site-operation') {
                title = to.query.type === 'create' ? '新增场地' : '场地编辑'
            } else if (to.name === 'learnDrivingTypeList-operation') {
                title = to.query.type === 'create' ? '新增学车类型' : '学车类型编辑'
            } else {
                title = to.meta.title
            }
            if (!['/login', '/', ''].includes(to.path as string)) {
                this._routerStore.setHeaderRouter({
                    path: to.path,
                    title: title,
                    activePath: to.meta.hidden ? to.meta.activePath : '',
                    hidden: to.meta.hidden,
                    query: to.redirectedFrom ? to.redirectedFrom.query : to.query,
                    params: to.redirectedFrom ? to.redirectedFrom.params : to.params
                })
            }
        }
    }
}

export default Limit
